
package kaizone.haining.chaobohui.fragment;

import kaizone.haining.chaobohui.R;
import kaizone.haining.chaobohui.activity.CBHActivity;
import kaizone.haining.chaobohui.widget.CBHBotttom;
import kaizone.haining.chaobohui.widget.CBHHead;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class CBHContentFragment extends Fragment implements CBHBotttom.OnClickListener,
        CBHHead.OnLeftImageClickListener {

    private FragmentManager mFragmentManager;

    private CBHHead mHead;

    private Fragment mContent;

    private CBHBotttom mBotttom;

    public CBHContentFragment() {

    }

    public CBHContentFragment(FragmentManager mgr) {
        mFragmentManager = mgr;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (mFragmentManager == null) {
            mFragmentManager = ((CBHActivity) getActivity()).getSupportFragmentManager();
        }
        if (savedInstanceState != null)
            mContent = mFragmentManager.getFragment(savedInstanceState, "mContent");
        if (mContent == null)
            mContent = CBHHomeFragment.newInstance(mFragmentManager);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // return super.onCreateView(inflater, container, savedInstanceState);
        View view = (View) inflater.inflate(R.layout.cbh_fragment_content, null);
        mHead = (CBHHead) view.findViewById(R.id.head);
        mHead.setLeftImageListener(this);
        // mContent = (Fragment) view.findViewById(R.id.body);
        mFragmentManager.beginTransaction().replace(R.id.body, mContent).commit();
        mBotttom = (CBHBotttom) view.findViewById(R.id.bottom);
        mBotttom.setListener(this);
        return view;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mFragmentManager.putFragment(outState, "mContent", mContent);
    }

    public void switchContent(Fragment fragment) {
        mContent = null;
        mContent = fragment;
        mFragmentManager.beginTransaction().replace(R.id.body, fragment).commit();
    }

    public void addContentToBackStack(Fragment fragment) {
        mContent = null;
        mContent = fragment;
        FragmentTransaction transaction = mFragmentManager.beginTransaction();
        transaction.replace(R.id.body, fragment).addToBackStack(null).commit();
    }

    public void reomveContent(Fragment fragment) {
        mFragmentManager.beginTransaction().addToBackStack(null).remove(fragment).commit();
    }

    public void setFragmentManager(FragmentManager fm) {
        mFragmentManager = fm;
    }

    public void setTitle(String text) {
        mHead.setText(text);
    }

    public void setShowMenu() {
        mHead.setLeftImageMenuState();
    }

    public void setTitleLeftImageResource(int resId) {
        mHead.setLeftImageResource(resId);
    }

    @Override
    public void onLeftImageClick(View v) {
        if (mContent instanceof DetailUsersFragment) {
            setShowMenu();
            setTitle(getString(R.string.grzx));
            switchContent(CBHUsersFragment.newInstance());
            return;
        }
        if (getActivity() instanceof CBHActivity) {
            ((CBHActivity) getActivity()).showMenu();
        }
    }

    @Override
    public void onShouYe() {
        setTitle(getString(R.string.menu_cbh));
        setShowMenu();
        switchContent(CBHHomeFragment.newInstance(mFragmentManager));
    }

    @Override
    public void onTuShuo() {
        setTitle(getString(R.string.tscc));
        setShowMenu();
        switchContent(CBHTuShuoFragment.newInstance(mFragmentManager));
    }

    @Override
    public void onHuodong() {
        setTitle(getString(R.string.hdzx));
        setShowMenu();
        switchContent(CBHHuodongFragment.newInstance(mFragmentManager));
    }

    @Override
    public void onWode() {
        setTitle(getString(R.string.grzx));
        setShowMenu();
        switchContent(CBHUsersFragment.newInstance());
    }

    public static CBHContentFragment newInstance(FragmentManager fm) {
        final CBHContentFragment f = new CBHContentFragment();
        f.setFragmentManager(fm);
        return f;
    }

    public static CBHContentFragment newInstance2(FragmentManager fm) {
        final CBHContentFragment f = new CBHContentFragment(fm);
        return f;
    }

}
